// pages/shopcart/shopcart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts:[],
    num: 0,
    bookPrice: 37.8,
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },
  setCart(cart){
    let allChecked=true
    let totalPrice=0;
    let totalNum=0;
    let num=this.data.num;
    cart.forEach(v => {
          if(v.checked){
            totalPrice+=((v.quantity+num)*v.book_info.price);
            totalNum+=(v.quantity+num);
          }else{
            allChecked=false
          }
        });
        allChecked=cart.length!=0?allChecked:false;
    this.setData({
      carts: cart,
      allChecked: allChecked,
      totalPrice: totalPrice,
      totalNum:totalNum
    })
    wx.setStorageSync('cart', cart)
  },
  handleAllChange(){
    let cart=wx.getStorageSync('cart');
    let allCheccked=!this.data.allChecked;
    cart.forEach(v=>v.checked=allCheccked);
    console.log(cart)
    this.setCart(cart);
  },
  handleChange(e){
    const bookId=e.currentTarget.dataset.id;
    console.log(bookId)
    let cart=wx.getStorageSync('cart');
    console.log(cart)
    let index=cart.findIndex(v=>v.bookId===bookId);
    console.log(index)
    console.log(cart[index])
    cart[index].checked=!cart[index].checked;
    this.setCart(cart)
  },
  getCartList(){
    const userId = wx.getStorageSync('userId')
    wx.request({
      url: `http://localhost:8089/cart/list?userId=${userId}`,
      data: {
        "userId": userId,
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      dataType: "json",
      success: (res)=>{
         console.log(res.data)
         wx.setStorageSync('cart', res.data.data)
         const cartA =wx.getStorageSync('cart');
         let cartB=cartA.map(v=>{
          return Object.assign(v,{checked: true})
          });
          console.log(cartB)
          this.setCart(cartB)
      },
    })
  },
  addNum(e){
    console.log(e)
    let opr=e.currentTarget.dataset.opr;
    let id = e.currentTarget.dataset.id;
    let cart =wx.getStorageSync('cart');
    console.log(cart)
    const index=cart.findIndex(v=>v.bookId===id);
    cart[index].quantity+=parseInt(opr);
    this.setCart(cart)
  },
  jianNum(e){
    console.log(e)
    let opr=e.currentTarget.dataset.opr;
    let id = e.currentTarget.dataset.id;
    let cart =wx.getStorageSync('cart');
    const index=cart.findIndex(v=>v.bookId===id);
    cart[index].quantity+=parseInt(opr);
    this.setCart(cart)
  },
  gotobuy(){
    wx.removeStorageSync('orderitem')
    let cart=wx.getStorageSync('cart');
    console.log(cart)
    cart.forEach(v => {
      if(!v.checked){
        const index=cart.findIndex(v=>v.checked===false);
        cart.splice(index,1)
      }
    });
    console.log(cart)
    wx.setStorageSync('orderitem', cart)
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCartList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getCartList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const cartB =this.data.carts;
    this.setCart(cartB)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getCartList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})